using UnityEngine;
using UnityEngine.UI;
using UnityEditor;
using UnityEditorInternal;
using System.Collections.Generic;

[CustomEditor(typeof(ButtonMultiple))]
public class ButtonMultipleEditor : Editor
{
	public ButtonMultiple buttonTarget;

	private void OnEnable()
	{
		buttonTarget = target as ButtonMultiple;
	}

	public override void OnInspectorGUI()
	{
		EditorGUILayout.BeginHorizontal();
		{
			if (GUILayout.Button("Test Normal", GUILayout.Width(100f)))
			{
				TestElements(ButtonMultiple.ButtonState.Normal);
			}
			if (GUILayout.Button("Test Highlight", GUILayout.Width(100f)))
			{
				TestElements(ButtonMultiple.ButtonState.Highlighted);
			}
			if (GUILayout.Button("Test Press", GUILayout.Width(100f)))
			{
				TestElements(ButtonMultiple.ButtonState.Pressed);
			}
			if (GUILayout.Button("Test Disabled", GUILayout.Width(100f)))
			{
				TestElements(ButtonMultiple.ButtonState.Disabled);
			}
		}
		EditorGUILayout.EndHorizontal();

		foreach (ButtonMultiple.ButtonEvent evt in buttonTarget.m_Events)
		{
			if (DrawButtonEvent(evt))
			{
				buttonTarget.m_Events.Remove(evt);
				break;
			}
		}

		EditorGUILayout.BeginHorizontal("box");
		{
			GameObject obj = (GameObject)EditorGUILayout.ObjectField(null, typeof(GameObject), true);
			EditorGUILayout.LabelField("Add Object");

			if (obj != null)
			{
				buttonTarget.m_Events.Add(new ButtonMultiple.ButtonEvent(obj));
			}
		}
		EditorGUILayout.EndHorizontal();

		EditorGUILayout.PropertyField(this.serializedObject.FindProperty("m_OnClick"), true, GUILayout.MinWidth(50));

		if (GUI.changed)
		{
			EditorUtility.SetDirty(buttonTarget);
		}
	}

	private bool DrawButtonEvent(ButtonMultiple.ButtonEvent _evt)
	{
		List<string> _parameters = null;
		EditorGUILayout.BeginVertical("box");
		{
			EditorGUILayout.BeginHorizontal();
			{
				string arrowDown = ((char)0x25BC).ToString();
				string arrowRight = ((char)0x25B6).ToString();
				string buttonText = _evt.m_Toggle ? arrowRight + " " + (_evt.m_MainObj ? _evt.m_MainObj.name : "null") : arrowDown + " Object : ";
				if (GUILayout.Button(buttonText, EditorStyles.label))
				{
					_evt.m_Toggle = !_evt.m_Toggle;
				}
				
				if (!_evt.m_Toggle)
				{
					_evt.m_MainObj = (GameObject)EditorGUILayout.ObjectField(_evt.m_MainObj, typeof(GameObject), true);
				}


				if (GUILayout.Button("X", GUILayout.Width(20f)))
				{
					return true;
				}
			}
			EditorGUILayout.EndHorizontal();

			if (_evt.m_MainObj == null || _evt.m_Toggle)
			{
				EditorGUILayout.EndVertical();
				return false;
			}

			if(_evt.m_UseAnimator)
			{
				if (_evt.m_Triggers == null || _evt.m_Triggers.Length < 4)
				{
					_evt.m_Triggers = new string[4];
				}

				_parameters = ExtractParameters(_evt.m_MainObj.GetComponent<Animator>());
			}

			EditorGUILayout.BeginHorizontal();
			{
				_evt.m_ChangeColor = EditorGUILayout.ToggleLeft("Change Color", _evt.m_ChangeColor, GUILayout.Width(100f));
				
				_evt.m_PunchScale = EditorGUILayout.ToggleLeft("Punch Scale", _evt.m_PunchScale, GUILayout.Width(100f));
				
				_evt.m_SwapSprite = EditorGUILayout.ToggleLeft("Swap Sprite", _evt.m_SwapSprite, GUILayout.Width(100f));
				if (_evt.m_SwapSprite && _evt.m_MainObj.GetComponent<Image>() == null)
				{
					_evt.m_SwapSprite = false;
					Debug.LogError("There is no component \"Image\" on object " + _evt.m_MainObj);
				}

				_evt.m_UseAnimator = EditorGUILayout.ToggleLeft("Use Animator", _evt.m_UseAnimator, GUILayout.Width(150f));
				if (_evt.m_UseAnimator && _evt.m_MainObj.GetComponent<Animator>() == null)
				{
					_evt.m_UseAnimator = false;
					Debug.LogError("There is no component \"Animator\" on object " + _evt.m_MainObj);
				}
			}
			EditorGUILayout.EndHorizontal();

			if (_evt.m_Colors == null || _evt.m_Colors.Length < 4)
			{
				_evt.m_Colors = new Color[4] { Color.white, Color.white, Color.white, Color.white };
			}

			if (_evt.m_SwapSprite && (_evt.m_Sprites == null || _evt.m_Sprites.Length < 4))
			{
				_evt.m_Sprites = new Sprite[4];
			}

			if (_evt.m_PunchScale && (_evt.m_PunchValues == null || _evt.m_PunchValues.Length < 4))
			{
				_evt.m_PunchValues = new float[4];
			}
			
			EditorGUILayout.BeginHorizontal();
			{
				DrawElement(_evt, ButtonMultiple.ButtonState.Normal, _parameters);
				DrawElement(_evt, ButtonMultiple.ButtonState.Highlighted, _parameters);
			}
			EditorGUILayout.EndHorizontal();
			EditorGUILayout.BeginHorizontal();
			{
				DrawElement(_evt, ButtonMultiple.ButtonState.Pressed, _parameters);
				DrawElement(_evt, ButtonMultiple.ButtonState.Disabled, _parameters);
			}
			EditorGUILayout.EndHorizontal();

		}
		EditorGUILayout.EndVertical();

		return false;
	}

	private void DrawElement(ButtonMultiple.ButtonEvent _evt, ButtonMultiple.ButtonState _state, List<string> _parameters)
	{
		EditorGUILayout.BeginVertical();
		{
			EditorGUILayout.LabelField(_state.ToString(), GUILayout.Width(100f));

			EditorGUILayout.BeginHorizontal();
			if (_evt.m_ChangeColor)
			{
				_evt.m_Colors[(int)_state] = EditorGUILayout.ColorField(_evt.m_Colors[(int)_state]);
			}
			if (_evt.m_PunchScale)
			{
				_evt.m_PunchValues[(int)_state] = EditorGUILayout.FloatField(_evt.m_PunchValues[(int)_state], GUILayout.Width(50));
			}
			EditorGUILayout.EndHorizontal();

			if (_evt.m_SwapSprite)
			{
				_evt.m_Sprites[(int)_state] = (Sprite)EditorGUILayout.ObjectField(_evt.m_Sprites[(int)_state], typeof(Sprite), true);
			}

			if(_evt.m_UseAnimator)
			{
				int _index = EditorGUILayout.Popup(_parameters.IndexOf(_evt.m_Triggers[(int)_state]), _parameters.ToArray());
				if(_index != _parameters.IndexOf(_evt.m_Triggers[(int)_state]))
				{
					_evt.m_Triggers[(int)_state] = _parameters[_index];
				}
			}
		}
		EditorGUILayout.EndVertical();
	}

	private void TestElements(ButtonMultiple.ButtonState _state)
	{
		foreach (ButtonMultiple.ButtonEvent evt in buttonTarget.m_Events)
		{
			evt.ApplyEvent(_state);
		}
	}

	private List<string> ExtractParameters(Animator _animator)
	{
		List<string> _list = new List<string>();

		AnimatorController controller = _animator.runtimeAnimatorController as AnimatorController;

		for(int i = 0 ; i < controller.parameterCount ; i++)
		{
			if(controller.GetParameter(i).type == AnimatorControllerParameterType.Trigger)
			{
				_list.Add(controller.GetParameter(i).name);
			}
		}

		return _list;
	}
}
